<?php
require_once('includes/config.php');
require_once('includes/functions/func.global.php');
require_once('includes/classes/class.template_engine.php');

// Connect to database
db_connect($config);

// Start the session
session_start();

// Initiate return var
$bury = array();
$bury[0] = 0;

// If no ID is specified return an error
if(!isset($_GET['i']))
{
	exit('0|'.$_GET['i']);
}

// If no ID is specified return an error
if(!$_GET['i'])
{
	exit('0|'.$_GET['i']);
}

// Check user is logged in
if(isset($_SESSION['duser']['id']))
{
	// Check they havn't already buried this story
	if(!isset($_SESSION['bury'][$_GET['i']]))
	{
		// Lookup buries table to see if they have already buried this
		$bury = mysql_num_rows(mysql_query("SELECT 1 FROM ".$config['db']['pre']."buries WHERE story_id='".validate_input($_GET['i'])."' AND user_id='".$_SESSION['duser']['id']."' LIMIT 1"));
	
		// Check that they havn't buried it
		if(!$bury)
		{
			// Update bury count on story
			mysql_query("UPDATE `".$config['db']['pre']."stories` SET `story_buries` = story_buries+1 WHERE `story_id` =".validate_input($_GET['i'])." LIMIT 1 ;");
			// Insert bury into digs table
			mysql_query("INSERT INTO `".$config['db']['pre']."buries` ( `story_id` , `user_id` ) VALUES ('".validate_input($_GET['i'])."', '".$_SESSION['duser']['id']."');");
		
			// Register that they have burried this story in session
			$_SESSION['bury'][$_GET['i']] = true;
			
			// Return success
			echo '1|'.$_GET['i'];
		}
		else
		{
			// User has already buried this story
			echo '2|'.$_GET['i'];
		}
	}
	else
	{
		// User has already buried this story
		echo '2|'.$_GET['i'];
	}
}
else
{
	// User isn't logged in
	echo '0|'.$_GET['i'];
}
?>